/* eslint-disable */

export const showPickArea = (_viewer, layerId, objId, positions, plotBgColor) => {
  var text = getMeasureTip2(_viewer, positions);
  let material, outlineMaterial;
  // if (material == null) {
  // material = Cesium.Color.fromCssColorString(plotBgColor).withAlpha(0.5);
  material = plotting.fullColor;
  // }
  // if (outlineMaterial == null) {
  outlineMaterial = new Cesium.PolylineDashMaterialProperty({
    dashLength: 16,
    // color: Cesium.Color.fromCssColorString('#00f').withAlpha(0.7)
    color: plotting.lineColor
  });
  // }
  var dynamicHierarchy = new Cesium.CallbackProperty(function () {
    if (positions.length > 2) {
      var pHierarchy = new Cesium.PolygonHierarchy(positions);
      return pHierarchy;
    } else {
      return null;
    }
  }, false);
  var outlineDynamicPositions = new Cesium.CallbackProperty(function () {
    if (positions.length > 1) {
      var arr = [].concat(positions);
      var first = positions[0];
      arr.push(first);
      return arr;
    } else {
      return null;
    }
  }, false);
  var num = positions.length;
  var last = positions[num - 1];
  var bData = {
    position: last,
    layerId,
    objId,
    shapeType: "AreaMeasure",
    label: {
      text,
      font: '16px "微软雅黑", Arial, Helvetica, sans-serif, Helvetica',
      fillColor: Cesium.Color.RED,
      outlineColor: Cesium.Color.SKYBLUE,
      outlineWidth: 1,
      style: Cesium.LabelStyle.FILL_AND_OUTLINE,
      heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    },
    polygon: new Cesium.PolygonGraphics({
      hierarchy: dynamicHierarchy,
      material: material,
      show: true
    }),
    polyline: {
      positions: outlineDynamicPositions,
      clampToGround: true,
      width: 2,
      material: outlineMaterial,
      show: true
    }
  };
  return _viewer.entities.add(bData);
}


function getMeasureTip2(_viewer, pntList) {
  var dis2d = _computeLineDis2d(pntList);
  var dis3d = _computeLineDis3d(pntList);
  dis2d = dis2d.toFixed(3);
  dis3d = dis3d.toFixed(3);
  var tip = "周长：" + dis3d + "千米";
  if (pntList.length > 2) {
    var area = _computeArea(_viewer, pntList);
    tip += "\n 面积：" + area.toFixed(3) + "平方千米";
  }
  return tip;
}
function _computeDis2d(c1, c2) {
  var dis = Cesium.Cartesian2.distance(c1, c2) / 1000;
  return dis;
}
function _computeDis3d(p1, p2) {
  var dis = Cesium.Cartesian3.distance(p1, p2) / 1000;
  return dis;
}
function _computeLineDis2d(pntList) {
  var total = 0;
  for (var i = 1; i < pntList.length; i++) {
    var p1 = pntList[i - 1];
    var p2 = pntList[i];
    var dis = _computeDis2d(p1, p2);
    total += dis;
  }
  return total;
}
function _computeLineDis3d(pntList) {
  var total = 0;
  for (var i = 1; i < pntList.length; i++) {
    var p1 = pntList[i - 1];
    var p2 = pntList[i];
    var dis = _computeDis3d(p1, p2);
    total += dis;
  }
  return total;
}
function _cartesian2LonLat(_viewer, cartesian) {
  //将笛卡尔坐标转换为地理坐标
  var cartographic = _viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
  //将弧度转为度的十进制度表示
  var pos = {
    lon: Cesium.Math.toDegrees(cartographic.longitude),
    lat: Cesium.Math.toDegrees(cartographic.latitude),
    alt: Math.ceil(cartographic.height)
  };
  return pos;
}
function _computeArea(_viewer, positions) {
  var arr = [];
  for (var i = 0; i < positions.length; i++) {
    var p = _cartesian2LonLat(_viewer, positions[i]);
    arr.push([p.lon, p.lat]);
  }
  arr.push(arr[0]); //终点和起点重合

  var polygon = turf.polygon([arr]);
  var area = turf.area(polygon) / 1000 / 1000;
  return area;
}
